package com.hbt.orderfood.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.hbt.orderfood.R;
import com.hbt.orderfood.adapter.MngFoodAdapter;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.Food;
import com.hbt.orderfood.fragment.FoodFragment;
import com.hbt.orderfood.utils.StringUtils;
import com.hbt.orderfood.utils.ToolBarUtils;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ManagerActivity extends AppCompatActivity {

    @BindView(R.id.page)
    ViewPager page;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.toolbar)
    LinearLayout toolbar;
    private MngFoodAdapter mngFoodAdapter;
    private List<FoodFragment> fragments = new LinkedList<>();
    private ToolBarUtils toolBarUtils;
    String[] strs = new String[]{StringUtils.MAIN_STR, StringUtils.VEG_STR, StringUtils.DRINK_STR};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);
        ButterKnife.bind(this);
        initData();
        initPage();
        createToolBar();
    }

    void createToolBar() {
        toolBarUtils = new ToolBarUtils();
        toolBarUtils.createToolBar(toolbar,strs);
        toolBarUtils.setmOnToolBarClickListener(new ToolBarUtils.OnToolBarClickListener() {
            @Override
            public void onToolBarClick(int position) {
                page.setCurrentItem(position);
                toolBarUtils.changeColor(position);
            }
        });
        toolBarUtils.changeColor(0);
    }

    void initData() {
        FoodFragment f1 = new FoodFragment(StringUtils.MAIN);
        FoodFragment f2 = new FoodFragment(StringUtils.VEG);
        FoodFragment f3 = new FoodFragment(StringUtils.DRINK);
        fragments.add(f1);
        fragments.add(f2);
        fragments.add(f3);
    }

    void initPage() {
        page.setAdapter(new MyAdapter(getSupportFragmentManager()));
        page.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                toolBarUtils.changeColor(position);
                title.setText(strs[position]);
                fragments.get(position).updateData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }//initPage

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {    //重载
        SubMenu submenu1 = menu.addSubMenu("退出登录");
        SubMenu submenu2 = menu.addSubMenu("添加报刊");
        SubMenu submenu3 = menu.addSubMenu("查看订单");
        return true;
    }//onCreateOptionsMenu

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getTitle().toString()) {
            case "退出登录":
                Intent intent = new Intent(ManagerActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
            case "添加报刊":
                createDialog();
                break;
            case "查看订单":
                Intent intent1 = new Intent(ManagerActivity.this, MngOrderActivity.class);
                startActivity(intent1);
                break;
        }
        return true;
    }//onOptionsItemSelected

    //添加食物弹出窗口
    void createDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.add_food, null);
        EditText addfood_name = v.findViewById(R.id.addfood_name);
        EditText addfood_price = v.findViewById(R.id.addfood_price);
        RadioButton main_radio = v.findViewById(R.id.main_radio);
        RadioButton veg_radio = v.findViewById(R.id.veg_radio);

        AlertDialog dialog = new AlertDialog.Builder(ManagerActivity.this)
                .setTitle("添加报刊")
                .setPositiveButton("添加", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = addfood_name.getText().toString();
                        int price = Integer.parseInt(addfood_price.getText().toString());
                        String type = StringUtils.DRINK;
                        if (main_radio.isChecked()) {
                            type = StringUtils.MAIN;
                        } else if (veg_radio.isChecked()) {
                            type = StringUtils.VEG;
                        }
                        Food food = new Food();
                        food.setName(name);
                        food.setPrice(price);
                        food.setType(type);
                        DBAdapter dbAdapter = new DBAdapter(ManagerActivity.this);
                        dbAdapter.insertFood(food);
                        updatePage();//更新fragment数据
                        Toast.makeText(ManagerActivity.this, "添加成功", Toast.LENGTH_LONG).show();
//                        mngFoodAdapter.setFoods(dbAdapter.getFoodsByType(StringUtils.MAIN));
//                        mngFoodAdapter.notifyDataSetChanged();
                    }
                })
                .setView(v)
                .create();
        dialog.show();
    }

    void updatePage() {
        int cur_item = page.getCurrentItem();
        fragments.get(cur_item).updateData();
//        for (FoodFragment f : fragments) {
//            if(f != null)
//            f.updateData();
//        }
    }

    class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    //将触碰的位置设置到各个fragment中
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN){
            Point point = new Point();
            point.x = (int)ev.getRawX();
            point.y = (int)ev.getRawY();
            for(FoodFragment f: fragments){
                f.setPoint(point);
            }
        }
        return super.dispatchTouchEvent(ev);
    }


}//class end
